
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";

export default async function (ctx: FunctionContext) {
  let id = ctx.body._id
  if (nw.util.isNull(id)){
    return fail("参数不合法")
  }
  //构建返回结果
  let result = {
    "activityRuleList": [],
    "skuInfoList": [],
    "couponInfoList": []
  }



  const p1 = nw.db.findListByWhereJson({
    dbName:"activityRule",
    whereJson:{deleted:0,activityId:id}
  })

  const p2 = nw.db.findListByWhereJson({
    dbName:"activitySku",
    whereJson:{deleted:0,activityId:id}
  })

  let [activityRule,activitySku] = await Promise.all([p1,p2])
  result.activityRuleList = activityRule

  if (!nw.util.isNull(activitySku)&&nw.util.isArray(activitySku)&&activitySku.length>0){
    let skuId = activitySku.map(item=>item.skuId)
    let skuInfo = await nw.db.findListByWhereJson({
      dbName:"skuInfo",
      whereJson:{deleted:0,_id:cloud.database().command.in(skuId)}
    })
    result.skuInfoList = skuInfo
  }

  return ok(result)





}
